package com.cy.pj.sys.server.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @DATE: 2021/3/18 10:32
 * @Author: LuckyH
 */

// 通过这个切面学习spring aop中各种通知方法的应用
@Aspect
@Component
public class SysTimeAspect {

    @Pointcut("@annotation(com.cy.pj.common.annotation.RequiredTime)")
    public void  doTime(){
    }

    @Before("doTime()")
    public void doBefore(JoinPoint jp){
        Object[] args = jp.getArgs();
        for (Object arg : args) {
            System.out.println(arg);
        }
        System.out.println("@Before");
    }

    @After("doTime()")
    public void doAfter(){
        System.out.println("@after");
    }

    @AfterReturning("doTime()")
    public void doAfterReturning(){
        System.out.println("@AfterReturning");
    }

    @AfterThrowing("doTime()")
    public void doAfterThrowing(){
        System.out.println("@AfterThrowing");
    }

    @Around("doTime()")
    public Object doAround(ProceedingJoinPoint jp) throws Throwable {
        try {
            System.out.println("@Around.Before");
            Object proceed = jp.proceed();
            System.out.println("@Around.AfterReturning");
            return proceed;
        }catch (Exception e){
            System.out.println("@Around.AfterThrowing");
            e.printStackTrace();
            throw e;
        }finally {
            System.out.println("@Around.After");
        }
    }
}
